package com.aperos;

import java.util.ArrayList;

import android.app.AlertDialog;
import android.content.Context;
import android.content.DialogInterface;
import android.content.Intent;
import android.graphics.drawable.Drawable;
import android.widget.LinearLayout;
import android.widget.TextView;

import com.google.android.maps.ItemizedOverlay;

public class BarItems extends ItemizedOverlay<Bar> {

	private ArrayList<Bar> listBars = new ArrayList<Bar>();
	private Context mContext;
	public static final String PREFS_NAME = "AperosPasChersPrefsFile";
	
	public BarItems(Drawable defaultMarker, Context context) {
		super(boundCenterBottom(defaultMarker)); 
		mContext = context;
	}

	@Override
	protected Bar createItem(int i) {
		return listBars.get(i);
	}
	

	@Override
	public int size() {
		return listBars.size();
	}
	
	public void addOverlay(Bar bar) {
		listBars.add(bar);
	    populate();
	}
	
	private static String pad(Integer c) {
		if (c == null) {
			return "00";
		} else if (c >= 10) {
	        return String.valueOf(c);
		} else {
	        return "0" + String.valueOf(c);
		}
	}
	
	@Override
	protected boolean onTap(int index) {
	  final Bar bar = listBars.get(index);
	  
	  LinearLayout layout = new LinearLayout(mContext);
	  layout.setOrientation(LinearLayout.VERTICAL);
	  
	  if (bar.getPriceBeer() != null) {
		  LinearLayout line = new LinearLayout(mContext);
		  line.setOrientation(LinearLayout.HORIZONTAL);
		  TextView tv_lbl_price_beer = new TextView(mContext);
		  tv_lbl_price_beer.setText(mContext.getString(R.string.lbl_price_beer_dialog));
		  TextView tv_priceBeer = new TextView(mContext);
		  tv_priceBeer.setText(" " + bar.getPriceBeer().toString() + " " + bar.getMoney());
		  line.addView(tv_lbl_price_beer);
		  line.addView(tv_priceBeer);
		  layout.addView(line);
	  }
	  
	  if (bar.getPriceCocktail() != null) {
		  LinearLayout line2 = new LinearLayout(mContext);
		  line2.setOrientation(LinearLayout.HORIZONTAL);
		  TextView tv_lbl_price_cocktail = new TextView(mContext);
		  tv_lbl_price_cocktail.setText(mContext.getString(R.string.lbl_price_cocktail_dialog));
		  TextView tv_priceCocktail = new TextView(mContext);
		  tv_priceCocktail.setText(" " + bar.getPriceCocktail().toString() + " " + bar.getMoney());
		  line2.addView(tv_lbl_price_cocktail);
		  line2.addView(tv_priceCocktail);
		  layout.addView(line2);
	  }
	  
	  String timeFormat = AperosPasChers.settings.getString("timeFormat", "24");
	  
	  if (bar.getHappyHourBeginHour() != null && bar.getHappyHourBeginMinute() != null && bar.getHappyHourEndHour() != null && bar.getHappyHourEndMinute() != null) {
		  String begin = "";
		  String end = "";
		  if (timeFormat.equals("12")) {
				if (bar.getHappyHourBeginHour() != null && bar.getHappyHourBeginHour() > 12 && bar.getHappyHourBeginHour() < 24) {
					begin = new StringBuilder()
					.append(pad(bar.getHappyHourBeginHour() - 12)).append(":")
					.append(pad(bar.getHappyHourBeginMinute())).append(" pm").toString();
				} else {
					begin = new StringBuilder()
					.append(pad(bar.getHappyHourBeginHour())).append(":")
					.append(pad(bar.getHappyHourBeginMinute())).append(" am").toString();
				}
				
				if (bar.getHappyHourEndHour() != null && bar.getHappyHourEndHour() > 12 && bar.getHappyHourEndHour() < 24) {
					end = new StringBuilder()
					.append(pad(bar.getHappyHourEndHour() - 12)).append(":")
					.append(pad(bar.getHappyHourEndMinute())).append(" pm").toString();
				} else {
					end = new StringBuilder()
					.append(pad(bar.getHappyHourEndHour())).append(":")
					.append(pad(bar.getHappyHourEndMinute())).append(" am").toString();
				}
			} else {
				begin = new StringBuilder()
					.append(pad(bar.getHappyHourBeginHour())).append(":")
					.append(pad(bar.getHappyHourBeginMinute())).toString();
				end = new StringBuilder()
					.append(pad(bar.getHappyHourEndHour())).append(":")
					.append(pad(bar.getHappyHourEndMinute())).toString();
			}
		  
			LinearLayout line3 = new LinearLayout(mContext);
			line3.setOrientation(LinearLayout.HORIZONTAL);
			TextView tv_lbl_happy_hour_begin = new TextView(mContext);
			tv_lbl_happy_hour_begin.setText(mContext.getString(R.string.lbl_happy_hour_dialog));
			TextView tv_happy_hour_begin  = new TextView(mContext);
			  
			tv_happy_hour_begin.setText(" " + begin + " - " + end);
			line3.addView(tv_lbl_happy_hour_begin);
			line3.addView(tv_happy_hour_begin);
			layout.addView(line3);
	  }

	  
	  if (bar.getPriceBeerHappyHour() != null) {
		  LinearLayout line5 = new LinearLayout(mContext);
		  line5.setOrientation(LinearLayout.HORIZONTAL);
		  TextView tv_lbl_price_beer_happy_hour = new TextView(mContext);
		  tv_lbl_price_beer_happy_hour.setText(mContext.getString(R.string.lbl_price_beer_happy_hour_dialog));
		  TextView tv_price_beer_happy_hour  = new TextView(mContext);
		  tv_price_beer_happy_hour.setText(" " + bar.getPriceBeerHappyHour().toString() + " " + bar.getMoney());
		  line5.addView(tv_lbl_price_beer_happy_hour);
		  line5.addView(tv_price_beer_happy_hour);
		  layout.addView(line5);
	  }
	  
	  if (bar.getPriceCocktailHappyHour() != null) {
		  LinearLayout line6 = new LinearLayout(mContext);
		  line6.setOrientation(LinearLayout.HORIZONTAL);
		  TextView tv_lbl_price_cocktail_happy_hour = new TextView(mContext);
		  tv_lbl_price_cocktail_happy_hour.setText(mContext.getString(R.string.lbl_price_cocktail_happy_hour_dialog));
		  TextView tv_price_cocktail_happy_hour  = new TextView(mContext);
		  tv_price_cocktail_happy_hour.setText(" " + bar.getPriceCocktailHappyHour().toString() + " " + bar.getMoney());
		  line6.addView(tv_lbl_price_cocktail_happy_hour);
		  line6.addView(tv_price_cocktail_happy_hour);
		  layout.addView(line6);
	  }
	
	  AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
	  dialog.setTitle(bar.getName());
	  dialog.setMessage(bar.getAddress()).setView(layout);
	 
	  dialog.setPositiveButton(mContext.getString(R.string.lbl_edit_bar_error_btn), new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int item) {
	        	Intent intent = new Intent(mContext, BarErrorEdit.class);
	        	Service.currentBar = bar;
	        	
	        	mContext.startActivity(intent);
		    }
		});
	  
	  dialog.setNeutralButton(mContext.getString(R.string.lbl_edit_btn), new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int item) {
	        	Intent intent = new Intent(mContext, BarEdit.class);
	        	intent.putExtra("edit", "edit");
	        	Service.currentBar = bar;
	        	
	        	mContext.startActivity(intent);
		    }
		});
	  dialog.setNegativeButton(mContext.getString(R.string.lbl_back_btn), new DialogInterface.OnClickListener() {
		    public void onClick(DialogInterface dialog, int item) {
	        	dialog.cancel();
		    }
		});
	  
	  if (bar.isInHappyHour()) {
		if (bar.getPriceBeerHappyHour() <= 3.5 && bar.getPriceBeerHappyHour() != null) {
			dialog.setIcon(mContext.getResources().getDrawable(R.drawable.beer_mug_yellow_hh_overlay));
  		} else if (bar.getPriceBeerHappyHour() <= 4.5) {
  			dialog.setIcon(mContext.getResources().getDrawable(R.drawable.beer_mug_orange_hh_overlay));
  		} else {
  			dialog.setIcon(mContext.getResources().getDrawable(R.drawable.beer_mug_red_hh_overlay));
  		}
	  } else {
  		if (bar.getPriceBeer() <= 3.5) {
  			dialog.setIcon(mContext.getResources().getDrawable(R.drawable.beer_mug_yellow_overlay));
  		} else if (bar.getPriceBeer() <= 4.5) {
  			dialog.setIcon(mContext.getResources().getDrawable(R.drawable.beer_mug_orange_overlay));
  		} else {
  			dialog.setIcon(mContext.getResources().getDrawable(R.drawable.beer_mug_red_overlay));
  		}
	  }
	  
	  
	  dialog.show();
	  
	  
	  return true;
	}

}
